﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using weikebuluo.DBUtility;
using System.Data;
using weikebuluo.Controllers.Cs.Customer;
using NVelocity.Context;
using NVelocity;

namespace weikebuluo.web.Customer.Ashx
{
    /// <summary>
    /// News 的摘要说明
    /// </summary>
    public class News : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            int _mid = 0;   //企业Id
            int _nid = 0;   //文章Id
            int.TryParse(context.Request.QueryString["userid"], out _mid);
            int.TryParse(context.Request.QueryString["nid"], out _nid);

            if (_mid != 0 && _nid != 0)
            {
                SqlParameter[] para = new SqlParameter[] { 
                    new SqlParameter("@mid",_mid),
                    new SqlParameter("@nid",_nid)
                };

                DataTable dtNews = DbHelperSQL.Query(@"Select [MName],[CopyRight],[Template],[NId],M.[MId],[NTitle],[NPicUrl],Convert(varchar(10),[NCreateDate],121) NCreateDate,
                                                       [NContent],N.[Description],N.[KeyWords],N.[NStartDate],N.[NEndDate],N.[NIndustryType],N.[NIndustryName],N.[NProvince],N.[NCity],N.[NArea],N.[NDnotTitle] 
                                                       From t_news N,t_set_view M where N.user_id=M.user_id and Nid=@nid and M.user_id=@mid and NStatus=1 and NContentType='Content'", para).Tables[0];

                DataTable dtMerchant = DbHelperSQL.Query("Select MId,[MName],[CopyRight],[Template],Description,MPhone,user_id,KeyWords,HomeShareImg from t_set_view where user_id=@mid", para).Tables[0];
                DataTable dtHomeIcon = DbHelperSQL.Query("select Id,Name,PicUrl,Href,Type,BgColor,Opacity,Color,BgImg,Name_Status,PicUrl_Status,BgImg_Status From t_icon where user_id=@mid and Status=1 order by case when sort is null then 1 else 0 end,sort", para).Tables[0];
                if (dtNews.Rows.Count > 0)
                {
                    DataRow drNews = dtNews.Rows[0];
                    DataRow drMerchant = dtMerchant.Rows[0];
                    //列表样式
                    string _newsCss = "/web/wechat/website/" + (string.IsNullOrEmpty(drNews["Template"].ToString()) ? "Default" : drNews["Template"].ToString()) + "/css/News.css"; ;//文章列表Css
                    
                    //菜单样式
                    string _menuCss = "/web/wechat/website/" + (string.IsNullOrEmpty(drNews["Template"].ToString()) ? "Default" : drNews["Template"].ToString()) + "/css/Menu.css";
                    

                    IContext template = new VelocityContext();
                    BaseTemp bt = new BaseTemp();
                    //设置参数
                    template.Put("merchant", drMerchant);
                    template.Put("icon", dtHomeIcon);
                    template.Put("news", drNews);
                    template.Put("newscss", _newsCss);
                    template.Put("menucss", _menuCss);

                    context.Response.Write(bt.WriterTemplate("/web/wechat/website/" + (string.IsNullOrEmpty(drNews["Template"].ToString()) ? "Defualt" : drNews["Template"].ToString()) + "/News.html", template, 0));
                }
                else
                {
                    context.Response.Write("内容不存在");
                }
            }
            else
            {
                context.Response.Write("参数不正确");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}